home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / graphics / a-g / beyondthedark / developer / source / hop / hop.c next >
Encoding:
C/C++ Source or Header  |  1995-12-21  |  5.9 KB  |  239 lines

  1. /* Hop Library */
  2.  
  3. #include <exec/memory.h>
  4. #include <exec/execbase.h>
  5. #include <graphics/gfxbase.h>
  6. #include <intuition/intuitionbase.h>
  7. #include <libraries/iffparse.h>
  8. #include <utility/tagitem.h>
  9.  
  10. #include <clib/macros.h>
  11.  
  12. #define __USE_SYSBASE 42
  13.  
  14. #include <proto/exec.h>
  15. #include <proto/graphics.h>
  16. #include <proto/intuition.h>
  17. #include <proto/utility.h>
  18.  
  19. #include <math.h>
  20. #include <stdio.h>
  21.  
  22. #include <btd.h>
  23.  
  24. struct IntuitionBase *IntuitionBase;
  25. struct GfxBase *GfxBase;
  26. struct Library *UtilityBase,*MathIeeeDoubBasBase,*MathIeeeDoubTransBase;
  27.  
  28. #define HOPTAG(o) (BTD_Client+(o))
  29.  
  30. #define HOP_Seconds HOPTAG(0)
  31.  
  32. #define MAX_SECONDS 360L /* seconds until a new graphic is plotted */
  33. #define DEF_SECONDS 20L
  34.  
  35. #define DEG45 (-0.707106781)
  36.  
  37. struct BTDInteger HopIntParams[] =
  38.  {
  39.   HOP_Seconds,"Seconds",BTDPT_INTEGER,DEF_SECONDS,1L,MAX_SECONDS,TRUE
  40.  };
  41.  
  42. struct BTDNode *HopParams[] = {&HopIntParams[0].BI_Node,NULL};
  43.  
  44. struct BTDInfo HopInfo =
  45.  {
  46.   BTDI_Revision,MAKE_ID('H','O','P',' '),
  47.   "Hop a long","based on Hop","Matthias Scheler",
  48.   HopParams
  49.  };
  50.  
  51. struct HopStruct
  52.  {
  53.   struct BTDDrawInfo *hs_BTDDrawInfo;
  54.   WORD   hs_Left,hs_Top,hs_MidX,hs_MidY;
  55.   LONG   hs_Seconds;
  56.   LONG   hs_Time;
  57.   DOUBLE hs_a,hs_b, hs_c, hs_mag; /* Pattern Points */
  58.   DOUBLE hs_x,hs_y;               /* Running Point */
  59.   DOUBLE hs_sx,hs_sy;
  60.   LONG   hs_RandN,hs_RandF,hs_RandI;
  61.  };
  62.  
  63. /* catch FP errors */
  64.  
  65. int _FPERR=0;
  66.  
  67. void __stdargs _CXFERR(int Code)
  68.  
  69. {
  70.  _FPERR=Code;
  71. }
  72.  
  73. /* library stuff */
  74.  
  75. char MyBlankerName[] = "hop.btd";
  76. char MyBlankerID[]   = "Hop Blanker V" VERSION "." REVISION " for BTD";
  77.  
  78. LONG MyBlankerLibInit(void)
  79.  
  80. {
  81.  if (GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",37L))
  82.   {
  83.    if (IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",37L))
  84.     {
  85.      if (UtilityBase=OpenLibrary("utility.library",37L))
  86.       {
  87.        if (MathIeeeDoubBasBase=OpenLibrary("mathieeedoubbas.library",37L))
  88.         {
  89.          if (MathIeeeDoubTransBase=OpenLibrary("mathieeedoubtrans.library",37L)) return TRUE;
  90.  
  91.          CloseLibrary (MathIeeeDoubBasBase);
  92.         }
  93.        CloseLibrary (UtilityBase);
  94.       }
  95.      CloseLibrary (&IntuitionBase->LibNode);
  96.     }
  97.    CloseLibrary (&GfxBase->LibNode);
  98.   }
  99.  return FALSE;
  100. }
  101.  
  102. void MyBlankerLibFree(void)
  103.  
  104. {
  105.  CloseLibrary (MathIeeeDoubTransBase);
  106.  CloseLibrary (MathIeeeDoubBasBase);
  107.  CloseLibrary (UtilityBase);
  108.  CloseLibrary (&IntuitionBase->LibNode);
  109.  CloseLibrary (&GfxBase->LibNode);
  110. }
  111.  
  112. /* random generator */
  113.  
  114. void __regargs InitRandom(struct HopStruct *HopStruct,ULONG Instance)
  115.  
  116. {
  117.  ULONG Time[2];
  118.  
  119.  CurrentTime (&Time[0],&Time[1]);
  120.  HopStruct->hs_RandN=(LONG)Time[0];
  121.  
  122.  if (Time[1]<1024L) Time[1]|=1;
  123.  else Time[1]>>=10;
  124.  Time[1]^=Instance;
  125.  
  126.  HopStruct->hs_RandF=4*Time[1]+1;
  127.  HopStruct->hs_RandI=2*Time[1]+1;
  128. }
  129.  
  130. WORD __regargs Random(struct HopStruct *HopStruct,WORD Max)
  131.  
  132. {
  133.  HopStruct->hs_RandN=HopStruct->hs_RandF*HopStruct->hs_RandN+HopStruct->hs_RandI;
  134.  if (HopStruct->hs_RandN<0L) HopStruct->hs_RandN=-HopStruct->hs_RandN;
  135.  
  136.  return (WORD)(HopStruct->hs_RandN%Max);
  137. }
  138.  
  139. /* implementation of library functions */
  140.  
  141. struct BTDInfo *QueryMyBlanker(void)
  142.  
  143. {
  144.  return &HopInfo;
  145. }
  146.  
  147. struct HopStruct *InitMyBlanker(struct TagItem *TagList)
  148.  
  149. {
  150.  struct BTDDrawInfo *BTDDrawInfo;
  151.  struct HopStruct *HopStruct;
  152.  ULONG *Error,Dummy,Instance;
  153.  
  154.  if ((BTDDrawInfo=(struct BTDDrawInfo *)
  155.                    GetTagData(BTD_DrawInfo,NULL,TagList))==NULL) return NULL;
  156.  Error=(ULONG *)GetTagData(BTD_Error,(ULONG)&Dummy,TagList);
  157.  if ((HopStruct=AllocVec(sizeof(struct HopStruct),MEMF_PUBLIC|MEMF_CLEAR))==NULL)
  158.   {
  159.    *Error=BTDERR_Memory;
  160.    return NULL;
  161.   }
  162.  Instance=GetTagData(BTD_Instance,0L,TagList);
  163.  
  164.  HopStruct->hs_BTDDrawInfo=BTDDrawInfo;
  165.  HopStruct->hs_Left=HopStruct->hs_BTDDrawInfo->BDI_Left;
  166.  HopStruct->hs_Top=HopStruct->hs_BTDDrawInfo->BDI_Top;
  167.  HopStruct->hs_MidX=HopStruct->hs_BTDDrawInfo->BDI_Width/2;
  168.  HopStruct->hs_MidY=HopStruct->hs_BTDDrawInfo->BDI_Height/2;
  169.  HopStruct->hs_Seconds=GetTagData(HOP_Seconds,DEF_SECONDS,TagList);
  170.  InitRandom (HopStruct,Instance);
  171.  
  172.  BTDDrawInfo->BDI_Red[BTDDrawInfo->BDI_Pens[0]]=255;
  173.  BTDDrawInfo->BDI_Green[BTDDrawInfo->BDI_Pens[0]]=255;
  174.  BTDDrawInfo->BDI_Blue[BTDDrawInfo->BDI_Pens[0]]=255;
  175.  BTDDrawInfo->BDI_Changed[BTDDrawInfo->BDI_Pens[0]]=TRUE;
  176.  SetAPen (BTDDrawInfo->BDI_RPort,BTDDrawInfo->BDI_Pens[0]);
  177.  
  178.  return HopStruct;
  179. }
  180.  
  181. void EndMyBlanker(struct HopStruct *HopStruct)
  182.  
  183. {
  184.  FreeVec (HopStruct);
  185. }
  186.  
  187. void AnimMyBlanker(struct HopStruct *HopStruct)
  188.  
  189. {
  190.  DOUBLE Help,NewY;
  191.  ULONG Seconds,Micros;
  192.  LONG X,Y;
  193.  
  194.  CurrentTime (&Seconds,&Micros);
  195.  if (Seconds>=HopStruct->hs_Time)
  196.   {
  197.    HopStruct->hs_Time=Seconds+HopStruct->hs_Seconds;
  198.    HopStruct->hs_a=Random(HopStruct,300)/100.0;
  199.    HopStruct->hs_b=Random(HopStruct,100)/100.0;
  200.    HopStruct->hs_c=Random(HopStruct,50)/100.0;
  201.    HopStruct->hs_mag=(1<<(Random(HopStruct,4)+5))*DEG45;
  202.    HopStruct->hs_x=0.0;
  203.    HopStruct->hs_y=0.0;
  204.    HopStruct->hs_sx=0.0;
  205.    HopStruct->hs_sy=0.0;
  206.  
  207.    EraseRect (HopStruct->hs_BTDDrawInfo->BDI_RPort,
  208.               HopStruct->hs_BTDDrawInfo->BDI_Left,
  209.               HopStruct->hs_BTDDrawInfo->BDI_Top,
  210.               HopStruct->hs_BTDDrawInfo->BDI_Left+HopStruct->hs_BTDDrawInfo->BDI_Width-1,
  211.               HopStruct->hs_BTDDrawInfo->BDI_Top+HopStruct->hs_BTDDrawInfo->BDI_Height-1);
  212.   }
  213.  
  214.  NewY=HopStruct->hs_a-HopStruct->hs_x;
  215.  
  216.  Help=fabs(HopStruct->hs_b*HopStruct->hs_x-HopStruct->hs_c);
  217.  
  218.  if (HopStruct->hs_x<0.0L) HopStruct->hs_x=HopStruct->hs_y+sqrt(Help);
  219.  else HopStruct->hs_x=HopStruct->hs_y-sqrt(Help);
  220.  
  221.  HopStruct->hs_y=NewY;
  222.  
  223.  HopStruct->hs_sx=HopStruct->hs_mag*(HopStruct->hs_x+HopStruct->hs_y);
  224.  HopStruct->hs_sy=HopStruct->hs_mag*(HopStruct->hs_y-HopStruct->hs_x);
  225.  
  226.  X=(LONG)HopStruct->hs_sx+HopStruct->hs_MidX;
  227.  Y=(LONG)HopStruct->hs_sy+HopStruct->hs_MidY;
  228.  if ((X>=0L)&&(X<HopStruct->hs_BTDDrawInfo->BDI_Width)&&
  229.      (Y>=0L)&&(Y<HopStruct->hs_BTDDrawInfo->BDI_Height))
  230.   WritePixel (HopStruct->hs_BTDDrawInfo->BDI_RPort,
  231.               HopStruct->hs_Left+X,HopStruct->hs_Top+Y);
  232. }
  233.  
  234. ULONG PenCountMyBlanker(struct TagItem *TagList)
  235.  
  236. {
  237.  return 1L;
  238. }
  239.